package com.eystar.pdc.util;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.eystar.ext.kit.ContainerKit;
import com.eystar.ext.kit.tree.BaseTree;
import com.eystar.ext.kit.tree.TreeKit;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;

/**
 * 此类是为了显示右侧菜单栏，如果要菜单栏选中，需要在Controller中index代码中添加如下信息
 * setAttr(Constant.MENU_REMARK_NAME, getPara(Constant.MENU_REMARK_NAME));
 * 
 * 这一块还是有问题的，就是如果是页面中的某个按钮点击刷新时，就不能选中了。。。。。。。。。。。。。以后修改，这个可以用全局过滤器来实现
 * 
 * 该问题于2016年7月9日解决，在AuthInterceptor中解决的
 * @author auth
 *
 */
public class MenuHelper {

	/**
	 * 输出顶部菜单
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String outTopMenu(HttpServletRequest request){
		String path = request.getContextPath();
		StringBuffer html = new StringBuffer();
		html.append("<ul class='nav navbar-nav'>");
		for (BaseTree tree : (List<BaseTree>) ContainerKit.get("resource_tree")) {
			if (!isVisiable(request, tree)) {
				continue;
			}
			List<BaseTree> subResources = tree.getChildren();
			if (subResources != null && !subResources.isEmpty()) { // 如果有子菜单
				html.append("<li class='dropdown-hover #" + tree.getId() + "'>");
					html.append("<a href='javascript:void(0)'>");
						html.append("<i class='ace-icon " + tree.getAttr("res_icon") + " bigger-160'></i>");
						html.append("&nbsp;&nbsp;").append(tree.getNodeName()).append("&nbsp;&nbsp;");
						html.append("<i class='arrow fa fa-angle-down'></i>");
					html.append("</a>");
				StringBuffer subHtml = new StringBuffer();
				loopTopMenu(subResources,tree,subHtml,request);
				html.append(subHtml);
				html.append("</li>");
			}else { // 如果没有子菜单
				html.append("<li class='#" + tree.getId() + "'>");
					html.append("<a href='" + path + "/" + tree.getAttr("res_url") + "'>");
						html.append("<i class='ace-icon " + tree.getAttr("res_icon") + " bigger-160'></i>");
						html.append("&nbsp;&nbsp;").append(tree.getNodeName()).append("&nbsp;&nbsp;");
					html.append("</a>");
				html.append("</li>");
			}
		}
		html.append("</ul>");
		
		String ret = html.toString();
		String menu = (String) request.getAttribute(LoginUtil.MENU_REMARK_NAME); // 当前链接对应的菜单
		String[] menuPath = menuIdsByRemark(menu);
		for (int i = 0; i < menuPath.length; i++) {
			ret = StrUtil.replace(ret, "#" + menuPath[i], "active");
		}
		return ret;
	}
	
	/**
	 * 如果根节点有子菜单，进入此循环
	 * @param subResources
	 * @param parentResource
	 * @param subHtml
	 * @param request
	 */
	public static void loopTopMenu(List<BaseTree> subResources,BaseTree parentResource,StringBuffer subHtml,HttpServletRequest request){
		String path = request.getContextPath();
		subHtml.append("<ul class='dropdown-menu dropdown-light-blue dropdown-caret'>");
		for (BaseTree subResource : subResources) {
			if (!isVisiable(request, subResource)) {
				continue;
			}
			// 如果有子菜单
			if (subResource.getChildren() != null && !subResource.getChildren().isEmpty() && parentResource.getId().equals(subResource.getParentId())) {
				subHtml.append("<li class='dropdown-hover #" + subResource.getId() + "'>");
					subHtml.append("<a href='javascript:void(0)'>");
						subHtml.append("<i class='ace-icon "+subResource.getAttr("res_icon")+" bigger-110 blue'></i>");
						subHtml.append("&nbsp;&nbsp;").append(subResource.getNodeName()).append("&nbsp;&nbsp;");
						subHtml.append("<i class='arrow fa fa-caret-right'></i>");
					subHtml.append("</a>");
					loopTopMenu(subResource.getChildren(),subResource,subHtml,request);
				subHtml.append("</li>");
			}else { // 如果没有子菜单
				subHtml.append("<li class='#" + subResource.getId() + "'>");
					subHtml.append("<a href='" + path + "/" + subResource.getAttr("res_url") + "'>");
						subHtml.append("<i class='ace-icon " + subResource.getAttr("res_icon") + " bigger-110 blue'></i>");
						subHtml.append("&nbsp;&nbsp;").append(subResource.getNodeName()).append("&nbsp;&nbsp;");
					subHtml.append("</a>");
				subHtml.append("</li>");
			}
		}
		subHtml.append("</ul>");
	}
	
	/**
	 * 输出左侧菜单
	 * @param request
	 * @return
	 */
	public static String outNavMenu(HttpServletRequest request) {
		String path = request.getContextPath();
		String menu = (String) request.getAttribute(LoginUtil.MENU_REMARK_NAME); // 当前链接对应的菜单
		if (StrUtil.isBlank(menu)) {
			return "";
		}
		List<BaseTree> trees = getBaseTreesByRootMenu(menu);
		if (trees != null && !trees.isEmpty()) {
			StringBuffer html = new StringBuffer();
			html.append("<ul class='nav nav-list'>");
			for (BaseTree tree : trees) {
				if (!isVisiable(request, tree)) {
					continue;
				}
				List<BaseTree> subResources = tree.getChildren();
				if (subResources != null && !subResources.isEmpty()) { // 如果有子菜单
					html.append("<li class='#"+tree.getId()+"'>");
						html.append("<a href='javascript:void(0)' class='dropdown-toggle'>");
							html.append("<i class='ace-menu-icon " + tree.getAttr("res_icon") + "'></i>");
							html.append("<span class='ace-menu-text'>");
								html.append("&nbsp;&nbsp;").append(tree.getNodeName()).append("&nbsp;&nbsp;");
							html.append("</span>");
							html.append("<b class='arrow fa fa-angle-down'></b>");
						html.append("</a>");
					StringBuffer subHtml = new StringBuffer();
					loopNavMenu(subResources,tree,subHtml,request);
					html.append(subHtml);
					html.append("</li>");
				}else { // 如果没有子菜单
					html.append("<li class='#"+tree.getId()+"'>");
						html.append("<a href='" + path + "/" + tree.getAttr("res_url") + "'>");
							html.append("<i class='ace-menu-icon " + tree.getAttr("res_icon") + "'></i>");
							html.append("<span class='ace-menu-text'>");
								html.append("&nbsp;&nbsp;").append(tree.getNodeName()).append("&nbsp;&nbsp;");
							html.append("</span>");
						html.append("</a>");
					html.append("</li>");
				}
			}
			html.append("</ul>");
			String ret = html.toString();
			String[] menuPath = MenuHelper.menuIdsByRemark(menu);
			for (int i = 0; i < menuPath.length; i++) {
				ret = StrUtil.replace(ret, "#" + menuPath[i], "active");
			}
			return ret;
		}
		return "";
	}
	
	/**
	 * 如果根节点有子菜单，进入此循环
	 * @param subResources
	 * @param parentResource
	 * @param subHtml
	 * @param request
	 */
	public static void loopNavMenu(List<BaseTree> subResources,BaseTree parentResource,StringBuffer subHtml,HttpServletRequest request){
		String path = request.getContextPath();
		subHtml.append("<ul class='submenu'>");
		for (BaseTree subResource : subResources) {
			if (!isVisiable(request, subResource)) {
				continue;
			}
			// 如果有子菜单
			if (subResource.getChildren() != null && !subResource.getChildren().isEmpty() && parentResource.getId().equals(subResource.getParentId())) {
				subHtml.append("<li class='dropdown-hover #"+subResource.getId()+"'>");
					subHtml.append("<a href='javascript:void(0)' class='dropdown-toggle'>");
						subHtml.append("<i class='ace-menu-icon "+subResource.getAttr("res_icon")+"'></i>");
						subHtml.append("&nbsp;&nbsp;").append(subResource.getNodeName()).append("&nbsp;&nbsp;");
						subHtml.append("<b class='arrow fa fa-angle-down'></b>");
					subHtml.append("</a>");
					loopNavMenu(subResource.getChildren(),subResource,subHtml,request);
				subHtml.append("</li>");
			}else { // 如果没有子菜单
				subHtml.append("<li class='#"+subResource.getId()+"'>");
					subHtml.append("<a href='"+path+"/"+subResource.getAttr("res_url")+"'>");
						subHtml.append("<i class='ace-menu-icon "+subResource.getAttr("res_icon")+"'></i>");
						subHtml.append("&nbsp;&nbsp;").append(subResource.getNodeName()).append("&nbsp;&nbsp;");
					subHtml.append("</a>");
				subHtml.append("</li>");
			}
		}
		subHtml.append("</ul>");
	}

	/**
	 * 判定该资源是否可见
	 * @param request
	 * @param tree
	 * @return
	 */
	public static boolean isVisiable(HttpServletRequest request,BaseTree tree){
		if ((Integer)tree.getAttr("status") == LoginUtil.DISPLAY_STATUS && PdcHelper.hasAuth(request, tree.getAttr("res_remark")+"_look")) {
			return true;
		}
		return false;
	}
	
	/**
	 * 根据菜单标识，获取所有父路径ID集合
	 * @param menuRemark
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static String[] menuIdsByRemark(String menuRemark){
		Map<String, BaseTree> map = (Map<String, BaseTree>) ContainerKit.get("resource_map");
		BaseTree tree = map.get(menuRemark);
		if (tree == null) {
			return new String[]{};
		}
		String res_path = tree.getAttr("res_path");
		return ArrayUtil.remove(StrUtil.split(res_path,"/"), 0);
	}
	
	/**
	 * 当前点击的菜单，获取到当前菜单的顶级菜单，在获取到顶级菜单下面的所有节点
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static List<BaseTree> getBaseTreesByRootMenu(String menuRemark) {
		Map<String, BaseTree> map = (Map<String, BaseTree>) ContainerKit.get("resource_map");
		BaseTree resources = map.get(menuRemark);
		if (resources == null) {
			return null;
		}
		String res_path = resources.getAttr("res_path");
		String id = resources.getAttr("id");
		String rootId = StrUtil.split(res_path, "/")[2];
		if (StrUtil.equals(id, rootId)) { // 如果当前点击的就是根节点，说明他没有子节点了，或者根据StringUtils.split(res_path,"/")截取后的数组长度=2也可以判定
			return null;
		}
		return TreeKit.getChildTreeObjects((List<BaseTree>) ContainerKit.get("resources"), rootId);
	}
}
